angular.module('imsApp').controller('promotionGroupController', ['$scope', '$rootScope', '$state', 'PromotionGroupManager', 'ImsDialog', function ($scope, $rootScope, $state, PromotionGroupManager, ImsDialog) {
    $scope.search = {
        campaignsId: $state.params.campaignsId,
        timeType: $state.params.timeType || 'day',
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10
    };
    $scope.paginationPromotion = {
        currentPage: $scope.search.pageNo,
        itemsPerPage: $scope.search.pageSize,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go("campaigns.promotionGroup", {
                campaignsId: $scope.search.campaignsId,
                pageNo: $scope.search.pageNo,
                pageSize: $scope.search.pageSize
            })

        }
    };
    $scope.deleteGroup = function () {
        var ids = [];
        $scope.lists.forEach(function (item) {
            if (item.checked) {
                ids.push(item.groupId)
            }
        })
        if (!ids.length) {
            return;
        }
        ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
            if (a == 'ok') {
                PromotionGroupManager.promotionGroupDelete(ids).then(function (result) {
                    $state.reload();
                })
            }
        })

    }
    $scope.changeStatus = function (item) {
        var status = "";
        if (item.status == 3) {
            status = "4";
        } else {
            status = "3";
        }
        PromotionGroupManager.promotionGroupStatusEdit(item.groupId, {status: status}).then(function (result) {
            $state.reload();
        })
    }


    if($scope.search.campaignsId){
        PromotionGroupManager.promotionGroupList($scope.search.campaignsId, [$scope.search.pageNo, $scope.search.pageSize]).then(function (result) {
            if (result.data.c == "s") {
                $scope.lists = result.data.pageList;
                $scope.paginationPromotion.totalItems = result.data.total;
                $scope.title = result.data.title;
                //直接从广告计划进广告推广组列表时会没有广告主的ID
                if ($scope.advId == 0) {
                    $scope.advId = $scope.title.split(" ")[0];
                }

            }

        })
    }



}])

